<?php
/**
 * 公共函数库
 * Created by PhpStorm.
 * User: rdg
 * Date: 2022/4/2
 * Time: 10:17
 */


use App\Exceptions\MessageException;

if (!function_exists("test"))
{
    function test($int)
    {
        return $int;
    }
}

if (!function_exists("logException"))
{
    function logException($class,Exception $e)
    {
        if (env('APP_ENV') == 'local')
        {
            var_dump($class . ' Print ' . get_class($e) .': ',$e->getMessage() );
            var_dump('Print Trace: ',$e->getTraceAsString() );
        }
        if (!($e instanceof MessageException))
        {
            logger('Catch ' . get_class($e) . ' in ' . $class . ' , line: ' .$e->getCode() . " Message: ".$e->getMessage() . "\r\n". 'Trace: ' . $e->getTraceAsString());
        }
    }
}

if (!function_exists("changeArrayKey"))
{
    function changeArrayKey(array $array,array $keyMapping)
    {
        $arr = [];
        foreach ($array as $key => $item)
        {
            if (isset($keyMapping[$key]))
            {
                $arr[$keyMapping[$key]] = $item;
            }else{
                $arr[$key] = $item;
            }
        }
        return $arr;
    }
}

if (!function_exists("getLastDayOfMonth")){
    // 根据出行开始日期获取月初最一天
    function getLastDayOfMonth($travelDate)
    {
        return date('Y-m-d', strtotime(date('Y-m-01', strtotime("{$travelDate} +1 month"))) - 86400);
    }
}

if (!function_exists("isDateTime")){
    /**
     * 是否为Y-m-d时间格式
     */
    function isDateTime($date) {

        if (!preg_match("/^([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})$/", $date)) {
            return false;
        }

        list($data['y'], $data['m'], $data['d']) = explode('-', $date);

        if (!checkdate($data['m'], $data['d'], $data['y'])) {
            return false;
        }
        return true;
    }
}

if (!function_exists("isJson")){
    //查询产品的sps类别,用于归属利润中心
    function isJson($string)
    {
        json_decode($string);
        return (json_last_error() == JSON_ERROR_NONE);
    }
}

if (!function_exists("dateSortDesc")){
    function dateSortDesc(array $rs)
    {
        uksort($rs,function($left,$right){
            if($left>$right)
            {
                return -1;
            }
            if($left==$right)
            {
                return 0;
            }
            if($left<$right)
            {
                return 1;
            }
        });
        return $rs;
    }
}

if (!function_exists("generateOrderNo")){
    function generateOrderNo()
    {
        //订单号主体
        $orderNo = date('YmdHis') . rand(10000000,99999999);

        $orderNoLen = strlen($orderNo);
        $orderNoSum = 0;
        for($i=0; $i<$orderNoLen; $i++){
            $orderNoSum += (int)(substr($orderNo,$i,1));
        }
        //唯一订单号
        $orderSn = $orderNo . str_pad((100 - $orderNoSum % 100) % 100,2,'0',STR_PAD_LEFT);

        return $orderSn;
    }
}

if (!function_exists('getMicroTime'))
{
    function getMicroTime()
    {
        //' '中间是一个空格
        list($microsecond , $time) = explode(' ', microtime());

        return $time + $microsecond;
    }
}

if (!function_exists('numberFormat'))
{
    function numberFormat($amount)
    {
        return "$" . number_format($amount);
    }
}
